package com.xmzs.midjourney.service.impl;

import com.xmzs.common.core.utils.MapstructUtils;
import com.xmzs.common.core.utils.StringUtils;
import com.xmzs.common.mybatis.core.page.TableDataInfo;
import com.xmzs.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.xmzs.midjourney.domain.bo.MjServerBo;
import com.xmzs.midjourney.domain.vo.MjServerVo;
import com.xmzs.midjourney.domain.MjServer;
import com.xmzs.midjourney.mapper.MjServerMapper;
import com.xmzs.midjourney.service.IMjServerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * MJ服务器信息配置Service业务层处理
 *
 * @author lixg
 * @date 2023-08-09
 */
@RequiredArgsConstructor
@Service
public class MjServerServiceImpl implements IMjServerService {

    private final MjServerMapper baseMapper;

    /**
     * 查询MJ服务器信息配置
     */
    @Override
    public MjServerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询MJ服务器信息配置列表
     */
    @Override
    public TableDataInfo<MjServerVo> queryPageList(MjServerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MjServer> lqw = buildQueryWrapper(bo);
        Page<MjServerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询MJ服务器信息配置列表
     */
    @Override
    public List<MjServerVo> queryList(MjServerBo bo) {
        LambdaQueryWrapper<MjServer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MjServer> buildQueryWrapper(MjServerBo bo) {

        LambdaQueryWrapper<MjServer> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGuildId() != null, MjServer::getGuildId, bo.getGuildId());
        lqw.eq(bo.getChannelId() != null, MjServer::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getBotToken()), MjServer::getBotToken, bo.getBotToken());
        return lqw;
    }

    /**
     * 新增MJ服务器信息配置
     */
    @Override
    public Boolean insertByBo(MjServerBo bo) {
        MjServer add = MapstructUtils.convert(bo, MjServer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改MJ服务器信息配置
     */
    @Override
    public Boolean updateByBo(MjServerBo bo) {
        MjServer update = MapstructUtils.convert(bo, MjServer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MjServer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除MJ服务器信息配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
